package com.example.hasee.zhcd.Activities.ChuanWu;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.zhcd.Adapters.LeavePortAdapter;
import com.example.hasee.zhcd.Base.BaseActivity;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.DateTimePickDialogUtil;
import com.example.hasee.zhcd.Util.GlobalParameter;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;
import com.example.hasee.zhcd.View.NoScrollListView;
import com.example.hasee.zhcd.View.PullToRefreshView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static com.example.hasee.zhcd.R.id.fberth;

/**
 * Created by hasee on 2018/5/28.
 */

public class LeavePortActivity extends BaseActivity implements
        PullToRefreshView.OnFooterRefreshListener, PullToRefreshView.OnHeaderRefreshListener {
    private HashMap<String, String> map = (HashMap<String, String>) GlobalParameter.get("loginmap");
    private TextView title;
    private ImageView back;
    private ImageView home;
    private Button btn_query;
    private EditText start_date;
    private EditText end_date;
    private PullToRefreshView mPullToRefreshView;
    private NoScrollListView lp_list;
    private ScrollView scrollView;
    private int mYear;
    private int mMonth;
    private int mDay;
    private String mMonthstr;
    private String mDaystr;
    private String mYearstr;
    private String dateString;
    private String startString;
    private String endString;
    private ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    private List<HashMap<String, String>> newlist = new ArrayList<HashMap<String, String>>();
    private LeavePortAdapter adapter;
    private Integer posotion;
    private String fshipyearnumvcr = "";
    private String fcgs;
    private LayoutInflater inflater;
    private int pagenum = 1;
    private EditText ship;
    private String shipString;

    @Override
    public int contentViewId() {
        return R.layout.activity_leave_port;
    }

    @Override
    public void set() {
        title.setText("离港船舶");
        back.setOnClickListener(this);
        home.setVisibility(View.INVISIBLE);
        start_date.setOnClickListener(this);
        end_date.setOnClickListener(this);
        btn_query.setOnClickListener(this);
        mPullToRefreshView.setOnHeaderRefreshListener(this);
        mPullToRefreshView.setOnFooterRefreshListener(this);
        mPullToRefreshView.setLastUpdated(new Date().toLocaleString());
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH) - 1;
        mDay = c.get(Calendar.DAY_OF_MONTH);
        start_date.setText(getOldDate(-30));
        end_date.setText(getTime());
        startString = start_date.getText().toString();
        endString = end_date.getText().toString();
        shipString = ship.getText().toString();
        Log.e("参数", startString + "  " + endString + "  " + pagenum + "  " + shipString);
        getlplist();
    }

    @Override
    public void init() {
        back = (ImageView) findViewById(R.id.back);
        home = (ImageView) findViewById(R.id.home);
        title = (TextView) findViewById(R.id.centertitle);
        btn_query = (Button) findViewById(R.id.btn_query);
        start_date = (EditText) findViewById(R.id.start_date);
        end_date = (EditText) findViewById(R.id.end_date);
        ship = (EditText) findViewById(R.id.ship);
        lp_list = (NoScrollListView) findViewById(R.id.lp_list);
        scrollView = (ScrollView) findViewById(R.id.scrollView);
        mPullToRefreshView = (PullToRefreshView) findViewById(R.id.main_pull_refresh_view);
    }

    public String getTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }

    public static String getOldDate(int distanceDay) {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = new Date();
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) + distanceDay);
        Date endDate = null;
        try {
            endDate = dft.parse(dft.format(date.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dft.format(endDate);
    }

    private void getlplist() {
        ProgressDialogUtils.createProgressDialog(LeavePortActivity.this, "正在加载列表...");
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strKSSJ", startString);
        map.put("strJSSJ", endString);
        map.put("strPageNum", pagenum + "");
        map.put("strShip", shipString);
        httpUtils.get("ZHCW_lgcbList", map, "ZHCW_lgcbList");
    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "Fail":
                ProgressDialogUtils.closeProgressDialog();
                Toast.makeText(LeavePortActivity.this, "操作失败，请检查网络！", Toast.LENGTH_LONG).show();
                break;
            case "ZHCW_lgcbList":
                Log.e("ZHCW_lgcbList", msg.getMsg());
                ProgressDialogUtils.closeProgressDialog();
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    if ("0".equals(rspcode)) {
                        JSONArray data = json.getJSONArray("data");
                        if (pagenum == 1) {
                            pagenum = pagenum + 1;
                            list.clear();
                            for (int i = 0; i < data.length(); i++) {
                                HashMap<String, String> lmap = new HashMap<String, String>();
                                lmap.put("fshipcode", data.getJSONObject(i).getString("fshipcode"));
                                lmap.put("fshipname", data.getJSONObject(i).getString("fshipname"));
                                lmap.put("fshipordernum", data.getJSONObject(i).getString("fshipordernum"));
                                lmap.put("fzygs", data.getJSONObject(i).getString("fzygs"));
                                lmap.put("flgdate", data.getJSONObject(i).getString("flgdate"));
                                lmap.put("fwbsx", data.getJSONObject(i).getString("fwbsx"));
                                lmap.put("fshipyearnumvcr", data.getJSONObject(i).getString("fshipyearnumvcr"));
                                lmap.put("fcgs", data.getJSONObject(i).getString("fcgs"));
                                lmap.put("fberth", data.getJSONObject(i).getString("fberth"));
                                list.add(lmap);
                            }
                            adapter = new LeavePortAdapter(LeavePortActivity.this, list);
                            lp_list.setAdapter(adapter);
                        } else {
                            pagenum = pagenum + 1;
                            if (data.length() == 0) {

                                Toast.makeText(getApplicationContext(), "无更多新消息！",
                                        Toast.LENGTH_LONG).show();
                                mPullToRefreshView.onFooterRefreshComplete();
                            } else {
                                newlist.clear();
                                for (int i = 0; i < data.length(); i++) {
                                    HashMap<String, String> lmap = new HashMap<String, String>();
                                    lmap.put("fshipcode", data.getJSONObject(i).getString("fshipcode"));
                                    lmap.put("fshipname", data.getJSONObject(i).getString("fshipname"));
                                    lmap.put("fshipordernum", data.getJSONObject(i).getString("fshipordernum"));
                                    lmap.put("fzygs", data.getJSONObject(i).getString("fzygs"));
                                    lmap.put("flgdate", data.getJSONObject(i).getString("flgdate"));
                                    lmap.put("fwbsx", data.getJSONObject(i).getString("fwbsx"));
                                    lmap.put("fshipyearnumvcr", data.getJSONObject(i).getString("fshipyearnumvcr"));
                                    lmap.put("fcgs", data.getJSONObject(i).getString("fcgs"));
                                    newlist.add(lmap);
                                }
                                for (int j = 0; j < newlist.size(); j++) {
                                    list.add(newlist.get(j));
                                    adapter.notifyDataSetChanged();
                                }

                            }
                        }
                        mPullToRefreshView.onHeaderRefreshComplete();
                        mPullToRefreshView.onFooterRefreshComplete();
                    } else {
                        Toast.makeText(getApplicationContext(), "无更多新消息！",
                                Toast.LENGTH_LONG).show();
                        mPullToRefreshView.onFooterRefreshComplete();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.home:
                GlobalParameter.JumpFrontPage(getApplicationContext());
                break;
            case R.id.start_date:
                String t1 = "";
                if (start_date.getText().toString().equals("")) {
                    t1 = getTime();
                } else {
                    t1 = start_date.getText().toString();
                }
                DateTimePickDialogUtil dialog1 = new DateTimePickDialogUtil(this,
                        t1);
                dialog1.dateTimePicKDialog(start_date);
                break;
            case R.id.end_date:
                String t2 = "";
                if (end_date.getText().toString().equals("")) {
                    t2 = getTime();
                } else {
                    t2 = end_date.getText().toString();
                }
                DateTimePickDialogUtil dialog2 = new DateTimePickDialogUtil(this,
                        t2);
                dialog2.dateTimePicKDialog(end_date);
                break;
            case R.id.btn_query:
                pagenum = 1;
                list.clear();
                startString = start_date.getText().toString();
                endString = end_date.getText().toString();
                shipString = ship.getText().toString();
                View errorview = inputCheck();
                if (errorview != null) {
                    errorview.setFocusable(true);
                    errorview.requestFocus();
                    errorview.setFocusableInTouchMode(true);
                    return;
                } else {
                    Log.e("参数", startString + "  " + endString + "  " + pagenum + "  " + shipString);
                    getlplist();
                }
                break;
        }
    }

    private View inputCheck() {
        if (startString.length() == 0) {
            return start_date;
        } else if (endString.length() == 0) {
            return end_date;
        }
        return null;
    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        getlplist();
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        pagenum = 1;
        getlplist();
    }

    @Override
    protected void onResume() {
        super.onResume();
        adapter = new LeavePortAdapter(LeavePortActivity.this, list);
        lp_list.setAdapter(adapter);
    }

}
